package cn.edu.nankai.bookstore.dao.impi;

import cn.edu.nankai.bookstore.dao.BookDao;
import cn.edu.nankai.bookstore.domain.po.Book;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BookDaoImpl extends GenericBaseDao implements BookDao {
    @Override
    public List<Book> findByName(String name) {
        List<Book> books = null;
        try {
            this.getConnection();
            String sql = "select * from book where name = ?";
            this.executeQuery(sql,name);
            if (rs != null) {
                books = new ArrayList<>();
                while (rs.next()) {
                    books.add(new Book(rs.getInt("book_id"),
                            rs.getString("name"),
                            rs.getDouble("price"))
                    );
                }
            }
            this.closeAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return books;
    }

    @Override
    public Book findById(Integer id) {
        Book book = null;
        try {
            this.getConnection();
            String sql = "select * from book where book_id = ?";
            executeQuery(sql, id);
            if (rs != null && rs.next()) {
                book = new Book(rs.getInt("book_id"),
                        rs.getString("name"),
                        rs.getDouble("price")
                );
            }
            this.closeAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return book;
    }

    @Override
    public List<Book> findAll() {
        List<Book> books = null;
        try {
            this.getConnection();
            String sql = "select * from book";
            this.executeQuery(sql);
            if (rs != null) {
                books = new ArrayList<>();
                while (rs.next()) {
                    books.add(new Book(rs.getInt("book_id"),
                            rs.getString("name"),
                            rs.getDouble("price"))
                    );
                }
            }
            this.closeAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return books;
    }

    @Override
    public List<Book> findBySql(String sql, Object... params) {
        List<Book> books = null;
        try {
            this.getConnection();
            this.executeQuery(sql, params);
            if (rs != null) {
                books = new ArrayList<>();
                while (rs.next()) {
                    books.add(new Book(rs.getInt("book_id"),
                            rs.getString("name"),
                            rs.getDouble("price"))
                    );
                }
            }
            this.closeAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return books;
    }

    @Override
    public int insert(Book book) {
        int res = 0;
        try {

            this.getConnection();
            String sql = "insert into book values(null,?,?)";
            executeInsert(sql, book.getName(), book.getPrice());
            res = this.result;
            this.closeAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return res;
    }

    @Override
    public int update(Book book) {
        int res = 0;
        try {
            this.getConnection();
            String sql = "update book set name = ? , price = ? where book_id = ?";
            executeUpdate(sql, book.getName(), book.getPrice(), book.getBookId());
            res = this.result;
            this.closeAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return res;
    }

    @Override
    public int delete(Integer id) {
        int res = 0;
        try {
            this.getConnection();
            String sql = "delete from book where book_id = ?";
            executeUpdate(sql, id);
            res = this.result;
            this.closeAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return res;
    }

    @Override
    public int delete(Book book) {
        if (book != null)
            return delete(book.getBookId());
        else
            return 0;
    }
}
